# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:张广勤
@Web site: https://www.tunan.wang
@Github:www.github.com
 
@File:tesseract0.py
@Time:2022/5/6 8:01

@Motto:不积跬步无以至千里，不积小流无以成江海！
"""

import pytesseract

from PIL import Image

im = Image.open(r'C:\Users\hsk\Pictures\微信图片_20220506083000.jpg')
# im=im.convert('RGB')
im=im.convert('L')#转为灰度图片
# im.show()
result = pytesseract.image_to_string(im,lang='chi_sim')

# print(result)

with open('a.txt','w',encoding='u8') as f:
    f.write(result)

with open('a.txt','r',encoding='u8') as f:
    str1=f.readlines()
    print(str1)
    # for num,li in enumerate(str1):
    #     if num in [2]:
    #         shi=li.split('，')[0].split(':')[1]#链式截取
    #         print(f'地市:{shi}')
    #     if num in [4,6,20]:
    #         li=li.split(' ')[0]
    #         print(f'{num}:{li}')
    #     if num in [12]:
    #         li0=li.split(' ')[0]
    #         li1=li.split(' ')[1]
    #         print(f'{num}:{li0} {li1}')

    # for num,li in enumerate(str1):
    #     if num in [2]:
    #         shi=li.split('，')[0].split(':')[1]#链式截取
    #         print(f'地市:{shi}')
    #     if num==4:
    #         name=li.split(' ')[0]
    #         print(f'姓名:{name}')
    #     if num==6:
    #         code=li.split(' ')[0]
    #         print(f'身份证:{code}')
    #     if num==20:
    #         color=li.split(' ')[0]
    #         print(f'码色:{color}')
    #     if num in [12]:
    #         time0=li.split(' ')[0]
    #         time1=li.split(' ')[1]
    #         print(f'时间:{time0} {time1}')

    for num, li in enumerate(str1):
        if num == 2:
            shi = li.split('，')[0].split(':')[1]  # 链式截取
            # print(f'地市:{shi}')
        if num == 4:
            name = li.split(' ')[0]
            # print(f'姓名:{name}')
        if num == 6:
            code = li.split(' ')[0]
            # print(f'身份证:{code}')
        if num == 20:
            color = li.split(' ')[0]
            # print(f'码色:{color}')
        if num ==12:
            time0 = li.split(' ')[0]
            time1 = li.split(' ')[1]
            time1=time1.replace('\n','')#去掉\n
            # print(f'时间:{time0} {time1}')
            # print(f'时间:{time1}')
    print('{',end='')
    print(f'city:"{shi}",name:"{name}",auth:"{code}",time:"{time0} {time1}",code:"{color}"',end='')
    print('}')
#写入数据库
